#include<stdio.h>
#include<stdlib.h>


int extend(int *b,int *k)
{
	int ele,i,flag;
	char x[1000];
	i=(*k);
	flag=0;
	printf("\nEnter the numbers one by one(press 'enter' after each number)\nINPUT 's' IN LOWER CASE TO STOP INPUT\n");
	if((*k)==0)
	{
		scanf(" %s",x);
		flag=strcmp(x,"s");
		ele=atoi(x);
		if(flag!=0)
		{
			b[0]=ele;
			(*k)=(*k)+1;
		}
		else
		{
			return 0;
		}
	}
	scanf(" %s",x);
	flag=strcmp(x,"s");
	while (flag!=0)
	{
		ele=atoi(x);
		i=(*k);
		b=realloc(b,((*k)+1)*sizeof(int));
		if(b==NULL)
		{
			printf("Memory allocation failed\nOperation not done\n");
		}
		else
		{
			b[i]=ele;
			(*k)=(*k)+1;
			scanf(" %s",x);
			flag=strcmp(x,"s");
		}
	}
	return 0;
}

void append(int *b2,int *k2)
{
	int n;
	printf("Enter the value to append:");
	scanf("%d",&n);
	if(*k2==0)
	{
		b2[0]=n;
		(*k2)=(*k2)+1;
		printf("\nvalue appended\n");
	}
	else
	{
		b2=realloc(b2,sizeof(int)*(*k2));
		if(b2==NULL)
		{
			printf("Memory allocation failed\noperation not done\n");
		}
		else
		{
			b2[*k2]=n;
			(*k2)=(*k2)+1;
			printf("\nValue Appended\n");
		}
	}
}

void insert1(int *b3,int *k3)
{
	int pos,i,num;
	printf("\nCurrent size of the list is %d\n",*k3);
	printf("Enter position where element is to be inserted(0-%d):",(*k3));
	scanf("%d",&pos);
	(*k3)=(*k3)+1;
	b3=realloc(b3,sizeof(int)*(*k3));
	if(b3==NULL)
	{
	printf("unsuccessful memory allocation\nOperation not done");
	}
	else
	{
		for(i=(*k3)-1;i>pos;i--)
		{
			b3[i]=b3[i-1];
		}
		printf("\nEnter value to be inserted:");
		scanf("%d",&num);
		b3[pos]=num;
	}
}

void pop(int *b4,int *k4)
{
	int i,pos,num;
	printf("\nEnter the index of the element to be removed:");
	scanf("%d",&pos);
	num=b4[pos];
	for (i=pos+1;i<(*k4);i++)
	{
		b4[i-1]=b4[i];
	}
	(*k4)=(*k4)-1;
	printf("\nValue removed==%d\n\n",num);
}

void remove1(int *b9,int *k9)
{
	int num,i,flag=0,pos;
	printf("\nEnter value to be removed:");
	scanf("%d",&num);
	for(i=0;i<(*k9);i++)
	{
		if(b9[i]==num)
		{
			flag=1;
			pos=i;
			break;
		}
	}
	if(flag==0)
	{
		printf("\n\nValue not found in List\n\n");
	}
	else
	{
		for(i=pos;i<((*k9)-1);i++)
		{
			b9[i]=b9[i+1];
		}
		(*k9)=((*k9)-1);
		printf("\n\nValue removed\n\n");
	}
}

void reverse(int *b5,int k5)
{
	int len=(k5),temp;
	int i=0,j=(k5)-1;
	for(i=0,j=(k5)-1;i<j;i++,j--)
	{
		temp=b5[i];
		b5[i]=b5[j];
		b5[j]=temp;
	}
}

void sort(int *b6,int k6)
{
	int i,j,Temp,a;
	for(i=1; i<k6; i++)
	{
		Temp = b6[i];
		j = i-1;
		while(Temp<b6[j] && j>=0)
		{
			b6[j+1] = b6[j];
			j = j-1;
		}
		b6[j+1] = Temp;
	}
	printf("\n  Sort in  \n");
	printf("   1.Ascending Order\n");
	printf("   2.Descending Order\n");
	printf("Enter Choice:");
	scanf("%d",&a);
	switch(a)
	{
		case 1:{printf("\nList Sorted\n");break;}
		case 2:{reverse(b6,k6);printf("\nList Sorted\n");break;}
		default:{printf("\nWrong Choice\n");break;}
	};
}

void search(int *b7,int k7)
{
	int i,pos,num,flag=0;
	printf("\nEnter value to be searched:");
	scanf("%d",&num);
	for(i=0;i<k7;i++)
	{
		if (b7[i]==num)
		{
			flag=1;
			break;
		}
	}
	if(flag==1)
	{
		printf("\nValue found at %d index\n",i);
	}
	else
	{
		printf("\nValue not found\n");
	}
}

void count(int *b8,int k8)
{
	int i,num,cou=0;
	printf("\nEnter value whose occurences are to be counted:");
	scanf("%d",&num);
	for(i=0;i<k8;i++)
	{
		if(b8[i]==num)
		{
			cou+=1;
		}
	}
	printf("There are %d occurences of %d.\n",cou,num);
}

void print(int *b1,int k1)
{
	int i=0;
	printf("\n\n[");
	for(i=0;i<k1-1;i++)
	{
		printf("%d,",b1[i]);
	}
	printf("%d]\n\n",b1[i]);
}

int main()
{
	int *a,j=0,no;
	char b='y';
	a=malloc(sizeof(int));
	if(a==NULL)
	{
		printf("Memory allocation failed\nExiting......");
	}
	else
	{
		do
		{
			printf("\n\n               Main Menu                \n");
			printf("              1.Extend the list           \n");
			printf("              2.Append the List           \n");
			printf("              3.Insert an element         \n");
			printf("              4.Pop an element         \n");
			printf("              5.Remove an element       \n");
			printf("              6.Reverse the list          \n");
			printf("              7.Sort the List             \n");
			printf("              8.Search an element         \n");
			printf("              9.Count no. of occurences   \n");
			printf("              10.Print                     \n");
			printf("              11.Exit                     \n");
			printf("              Enter your Choice:");
			scanf("%d",&no);
			switch(no)
			{
				case 1:{extend(a,&j);break;}
				case 2:{append(a,&j);break;}
				case 3:{insert1(a,&j);break;}
				case 4:{pop(a,&j);break;}
				case 5:{remove1(a,&j);break;}
				case 6:{reverse(a,j);printf("\nReversal Done\n");break;}
				case 7:{sort(a,j);break;}
				case 8:{search(a,j);break;}
				case 9:{count(a,j);break;}
				case 10:{print(a,j);break;}
				case 11:{return 0;break;}
			};
			printf("Do you want to continue?");
			scanf(" %c",&b);
		}while(b=='Y' || b=='y');
	}
	return 0;
}
